package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.enums.ChainCallBackStatusEnum;
import com.sc.nft.enums.UserChainLogStatusEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.ChainCallBackLog;
import com.sc.nft.mapper.ChainCallBackLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 玛特联盟链回调日志表(sc_chain_call_back_log)数据DAO
 *
 * @author Sun
 * @since 2023-07-04 15:44:10
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class ChainCallBackLogDao extends ServiceImpl<ChainCallBackLogMapper, ChainCallBackLog> {

    public List<ChainCallBackLog> getListByStatus(ChainCallBackStatusEnum status,Boolean isSuccess) {
        LambdaQueryWrapper<ChainCallBackLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChainCallBackLog::getStatus, status);
        wrapper.eq(ChainCallBackLog::getIsSuccess, isSuccess);
        wrapper.last("limit 100");
        return list(wrapper);
    }
    public List<ChainCallBackLog> getListByStatusWithLimit(ChainCallBackStatusEnum status,Boolean isSuccess,long limit ) {
        LambdaQueryWrapper<ChainCallBackLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChainCallBackLog::getStatus, status);
        wrapper.eq(ChainCallBackLog::getIsSuccess, isSuccess);
        wrapper.last("limit "+limit);
        return list(wrapper);
    }

    public Boolean updateStatusById(Long id, ChainCallBackStatusEnum status) {
        LambdaUpdateWrapper<ChainCallBackLog> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ChainCallBackLog::getId, id);
        wrapper.set(ChainCallBackLog::getStatus, status);
        return update(wrapper);
    }

    public Boolean updateStatusAndTransactionHashById(Long id, ChainCallBackStatusEnum chainCallBackStatusEnum, String transactionHash) {
        LambdaUpdateWrapper<ChainCallBackLog> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ChainCallBackLog::getId, id);
        wrapper.set(ChainCallBackLog::getStatus, chainCallBackStatusEnum);
        wrapper.set(ChainCallBackLog::getTransactionHash, transactionHash);
        return update(wrapper);
    }

    public List<ChainCallBackLog> failListByOneHour() {
        return baseMapper.failListByOneHour();
    }

    public boolean updateStatusAndIsSuccessById(Long id, ChainCallBackStatusEnum status, Boolean isSuccess) {
        LambdaUpdateWrapper<ChainCallBackLog> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ChainCallBackLog::getId, id);
        wrapper.set(ChainCallBackLog::getStatus, status);
        wrapper.set(ChainCallBackLog::getIsSuccess, isSuccess);
        return update(new ChainCallBackLog(),wrapper);
    }
}